// 学生提交作业
import express, {json} from "express";
import pool from "../../importantApi/db_connection.js";

const router = express.Router();
router.post("/", async (req, res) => {
    const data = req.body;
    if (!data.createtime) {
        let date = new Date()
        const year = date.getFullYear()
        const month = date.getMonth() + 1
        const day = date.getDate()
        const hour = date.getHours()
        const minute = date.getMinutes()
        data.createtime = `${year}-${String(month).padStart(2,'0')}-${String(day).padStart(2,'0')} ${String(hour).padStart(2,'0')}:${String(minute).padStart(2,'0')}`
    }
    try {
        let [result] = await pool.query(`
            select u.user_name as student_name
            from userinfo u
            where u.user_id = '${data.student_id}'
        `)
        const params = [
            data.homework_id,
            data.student_id,
            data.student_name = result[0].student_name,
            data.content,
            data.image_list,
            data.recorder_list,
            data.video_list,
            data.file_list,
            data.createtime]

        let sql = `
            insert into dowork (homework_id, student_id, student_name, content, image_list, recorder_list, video_list,
                                file_list,
                                createtime)
            values (?, ?, ?, ?, ?, ?, ?, ?, ?)
        `
        await pool.query(sql, params)
        return res.status(200).json({
            code: 200,
            msg: "作业提交成功"
        })
    } catch (err) {
        console.log(err)
        res.status(500).json({
            code: 500,
            error: err,
            msg: "提交作业错误：server error"
        })
    }
})
export default router;